package com.erpoa.controller;

import com.erpoa.service.IPingzhiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjimin
 * @Title: PingzhiController
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/7/1615:45
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "PingzhiController", tags = {"品质系统接口"})
@RestController
@RequestMapping("/pingzhi")
public class PingzhiController {

    private static final Logger logger = LoggerFactory.getLogger(PingzhiController.class);

    @Autowired
    private IPingzhiService iPingzhiService;

    @ApiOperation("获取序列号")
    @RequestMapping(value = "getSequence", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public String getSequence(@RequestParam(value = "sequence", required = true) String sequence) {
        return iPingzhiService.getSequence(sequence);
    }


    @ApiOperation("创建序列号")
    @RequestMapping(value = "createSequence", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public String createSequence(@RequestParam(value = "sequence", required = true) String sequence) {
        try {
            iPingzhiService.createSequence(sequence);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains("ORA-00955")) {
                return sequence + "已存在，请不要重复创建！";
            } else {
                return "创建序列号错误，请联系相关人员！";
            }

        }
        return null;
    }


    @ApiOperation("创建序列")
    @RequestMapping(value = "dropSequence", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public String dropSequence(@RequestParam(value = "sequence", required = true) String sequence) {
        try {
            iPingzhiService.dropSequence(sequence);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains("ORA-02289")) {
                return sequence + "已删除，请不要重复删除！";
            } else {
                return "删除序列号错误，请联系相关人员！";
            }

        }
        return null;
    }


    /**
     * 点击机台小组弹出combox
     *
     * @param fmachinecode
     * @param page
     * @param rows
     * @return
     */
    @ApiOperation("点击机台小组弹出combogrid")

    @RequestMapping(value = "getDataWithfmachinecode", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Map selectDataWithfmachinecode(@RequestParam(value = "ffactorycode",required = false)String ffactorycode,@RequestParam(value = "fmachinecode",required = false) String fmachinecode,@RequestParam(value = "fplanbegindate",required = false) String fplanbegindate,@RequestParam(value = "fplanenddate",required = false)String fplanenddate,@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageHelper.startPage(page, rows);
        Map<String, Object> resultMap = new HashMap<>();
        PageInfo<HashMap> pageInfo = new PageInfo<HashMap>(this.iPingzhiService.selectDataWithfmachinecode(ffactorycode,fmachinecode, fplanbegindate, fplanenddate));
        //总条数
        resultMap.put("total", pageInfo.getTotal());
        //数据
        resultMap.put("rows", pageInfo.getList());
        return resultMap;
    }


    /**
     * 查询用户权限
     *
     * @param hrno
     * @return
     */
    @ApiOperation("查询用户权限")
    @RequestMapping(value = "selecttcpgenbyhrno", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List selecttcpgenbyhrno(@RequestParam(value = "hrno",required = false) String hrno) {
        return this.iPingzhiService.selecttcpgenbyhrno(hrno);

    }


}
